package online.westonline.com.westonline.adapter;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.alamkanak.weekview.WeekViewEvent;
import com.easy.west.westclient.api.workplan.baen.Event;
import com.github.lzyzsd.circleprogress.ArcProgress;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import cn.nekocode.badge.BadgeDrawable;
import online.westonline.com.westonline.R;
import online.westonline.com.westonline.ui.WorkPlanInfoActivity;
import online.westonline.com.westonline.utils.UiUtlis;

/**
 * 日程列表视图
 * Created by xiatao on 2018/7/6.
 */

public class MyEventListAdpter extends RecyclerView.Adapter<MyEventListAdpter.MyEventViewHolder> {

    private List<Event> events = new ArrayList<>();
    private List<Event> parsedata = new ArrayList<>();
    public void setEvents(List<Event> events) {
        this.events = events;
        //处理顺序
        parseData();
    }

    private void parseData() {
        parsedata.clear();
        //上午
        for (Event event : events) {
            if (event.toWeekViewEvent().getStartTime().get(Calendar.HOUR_OF_DAY) < 12) {
                parsedata.add(event);
            }
        }
        //中午
        for (Event event : events) {
            if (event.toWeekViewEvent().getStartTime().get(Calendar.HOUR_OF_DAY) < 14 && event.toWeekViewEvent().getStartTime().get(Calendar.HOUR_OF_DAY) >=12) {
                parsedata.add(event);
            }
        }
        //下午
        for (Event event: events)
        {
            if (event.toWeekViewEvent().getStartTime().get(Calendar.HOUR_OF_DAY) >=14&&event.toWeekViewEvent().getStartTime().get(Calendar.HOUR_OF_DAY)<18)
            {
                parsedata.add(event);
            }
        }
        //晚上
        for (Event event:events)
        {
            if (event.toWeekViewEvent().getStartTime().get(Calendar.HOUR_OF_DAY)>=18)
            {
                parsedata.add(event);
            }
        }
    }

    @NonNull
    @Override
    public MyEventViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.event_item, parent, false);
        return new MyEventViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull final MyEventViewHolder holder, final int position) {
        //计算进度条
        initprogress(parsedata.get(position), holder);
        //判断时间范围
        Event event = parsedata.get(position);
        //正常时间范围设置
        holder.tvChanel.setText(event.getStarttime()+"~"+event.getEndtime());
        //预设值时间范围
        if (event.getStarttime().equals("9:00") && event.getEndtime().equals("12:00")) {
            holder.tvChanel.setText("上午");
        }
        if (event.getStarttime().equals("12:00") && event.getEndtime().equals("14:00")) {
            holder.tvChanel.setText("中午");
        }
        if (event.getStarttime().equals("14:00") && event.getEndtime().equals("18:00"))
        {
            holder.tvChanel.setText("下午");
        }
        if (event.getStarttime().equals("19:00") && event.getEndtime().equals("23:59"))
        {
            holder.tvChanel.setText("晚上");
        }
        if (event.getStarttime().equals("00:00")&&event.getEndtime().equals("23:59"))
        {
            holder.tvChanel.setText("全天");
        }
        //点击事件
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Intent intent = new Intent(holder.itemView.getContext(), WorkPlanInfoActivity.class);
                Bundle bundle = new Bundle();
                bundle.putSerializable("event", parsedata.get(position).toWeekViewEvent());
                intent.putExtras(bundle);
                holder.itemView.getContext().startActivity(intent);

            }
        });

    }

    //进度计算
    private void initprogress(Event event_net, MyEventViewHolder holder) {
        holder.tvEventContet.setText(event_net.getContent());
        WeekViewEvent event = event_net.toWeekViewEvent();
        long time_start;
        long time_end;
        //转换
        time_start = event.getStartTime().getTime().getTime();
        time_end = event.getEndTime().getTime().getTime();
        //计算进度条
        //1.当前日期是否与日程日期相同
        int targetday = event.getStartTime().get(Calendar.DAY_OF_MONTH);
        int today = Calendar.getInstance().get(Calendar.DAY_OF_MONTH);
        //获取现在的时间
        long nowtime = System.currentTimeMillis();
        //这里要加上月份了
        int targetmonth = event.getStartTime().get(Calendar.MONTH)+1;
        int nowmonth = Calendar.getInstance().get(Calendar.MONTH)+1;
        if (targetday < today) {
            //昨天的事情了
            //holder.eventArcProgress.setBottomText("日程已完成");
            BadgeDrawable drawable =
                    new BadgeDrawable.Builder()
                            .type(BadgeDrawable.TYPE_ONLY_ONE_TEXT)
                            .textSize(25)
                            .text1("已完成")
                            .textColor(UiUtlis.getColor(R.color.colorAppTheme))
                            .badgeColor(UiUtlis.getColor(R.color.green))
                            .build();
            holder.imgState.setBackground(drawable);
            holder.eventArcProgress.setProgress(100);
        }
        if (targetday == today) {
            //当天
            //条件为计算范围
            if (nowtime >= time_start && nowtime < time_end) {
                long totaltime = 0;
                int start_hour = event.getStartTime().get(Calendar.HOUR_OF_DAY);
                int start_minut = event.getStartTime().get(Calendar.MINUTE);
                int end_hor = event.getEndTime().get(Calendar.HOUR_OF_DAY);
                int end_minute = event.getEndTime().get(Calendar.MINUTE);
                if (start_hour == end_hor) {
                    //同一小时内
                    totaltime = (end_minute - start_minut) * 60 * 1000;
                }
                 else
                {
                    long hourtime = (end_hor - start_hour) * 3600 * 1000;
                    totaltime = hourtime;
                    if (end_minute == start_minut) {
                        totaltime = hourtime;
                    }
                    if (end_minute > start_minut) {
                        totaltime = hourtime + (end_minute - start_minut) * 60 * 1000;
                    }
                    if (end_minute < start_hour) {
                        totaltime = hourtime + (start_minut - end_hor) * 60 * 1000;
                    }
                }
                //获取现在的时间（时分）
                int nowhour = Calendar.getInstance().get(Calendar.HOUR_OF_DAY);
                int nowminute = Calendar.getInstance().get(Calendar.MINUTE);
                long nowtime1 = (nowhour * 3600 * 1000 + nowminute * 60 * 1000) - (start_hour * 3600 * 1000 + start_minut * 60 * 1000);
                long casttime = totaltime - nowtime1;
                int progress = 100 - (int) (casttime * 100 / totaltime);
                if (progress == 0 && totaltime > casttime) {
                    progress = 100;
                }
                BadgeDrawable drawable2 =
                        new BadgeDrawable.Builder()
                                .type(BadgeDrawable.TYPE_ONLY_ONE_TEXT)
                                .textSize(25)
                                .text1("进行中")
                                .textColor(UiUtlis.getColor(R.color.colorAppTheme))
                                .badgeColor(UiUtlis.getColor(R.color.TopicItemNodeColor))
                                .build();
                holder.imgState.setBackground(drawable2);
                holder.eventArcProgress.setProgress(progress);
                if (progress == 100) {
                    BadgeDrawable drawable =
                            new BadgeDrawable.Builder()
                                    .type(BadgeDrawable.TYPE_ONLY_ONE_TEXT)
                                    .textSize(25)
                                    .text1("已完成")
                                    .textColor(UiUtlis.getColor(R.color.colorAppTheme))
                                    .badgeColor(UiUtlis.getColor(R.color.green))
                                    .build();
                    holder.imgState.setBackground(drawable);
                }

            }
            if (nowtime >= time_end) {
                //已过日程时间
                holder.eventArcProgress.setProgress(100);
                BadgeDrawable drawable =
                        new BadgeDrawable.Builder()
                                .type(BadgeDrawable.TYPE_ONLY_ONE_TEXT)
                                .textSize(25)
                                .text1("已完成")
                                .textColor(UiUtlis.getColor(R.color.colorAppTheme))
                                .badgeColor(UiUtlis.getColor(R.color.green))
                                .build();
                holder.imgState.setBackground(drawable);
            }
            if (nowtime < time_start) {
                //日程未开始
                holder.eventArcProgress.setProgress(0);
                BadgeDrawable drawable =
                        new BadgeDrawable.Builder()
                                .type(BadgeDrawable.TYPE_ONLY_ONE_TEXT)
                                .textSize(25)
                                .text1("未开始")
                                .textColor(UiUtlis.getColor(R.color.colorAppTheme))
                                .badgeColor(UiUtlis.getColor(R.color.TabTextSelectColor))
                                .build();
                holder.imgState.setBackground(drawable);
            }
        }
        if (targetday > today) {
            holder.eventArcProgress.setProgress(0);
            BadgeDrawable drawable =
                    new BadgeDrawable.Builder()
                            .type(BadgeDrawable.TYPE_ONLY_ONE_TEXT)
                            .textSize(25)
                            .text1("未开始")
                            .textColor(UiUtlis.getColor(R.color.colorAppTheme))
                            .badgeColor(UiUtlis.getColor(R.color.TabTextSelectColor))
                            .build();
            holder.imgState.setBackground(drawable);
        }
        if (targetmonth<nowmonth)
        {
            //已过日程时间
            holder.eventArcProgress.setProgress(100);
            BadgeDrawable drawable =
                    new BadgeDrawable.Builder()
                            .type(BadgeDrawable.TYPE_ONLY_ONE_TEXT)
                            .textSize(25)
                            .text1("已完成")
                            .textColor(UiUtlis.getColor(R.color.colorAppTheme))
                            .badgeColor(UiUtlis.getColor(R.color.green))
                            .build();
            holder.imgState.setBackground(drawable);
        }
    }

    @Override
    public int getItemCount() {
        return parsedata.size();
    }

    class MyEventViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.img_state)
        ImageView imgState;
        @BindView(R.id.tv_event_contet)
        TextView tvEventContet;
        @BindView(R.id.tv_chanel)
        TextView tvChanel;
        @BindView(R.id.event_arc_progress)
        ArcProgress eventArcProgress;

        public MyEventViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }
}
